<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
     Function.prototype.bind2 = function (context){
        if(typeof this !== "function"){
           return new Error(11)
        }
        let self = this;
        let fNOP = function (){}
        // 获取bind2函数从第二个参数到最后一个参数
        let args = Array.prototype.slice.call(arguments,1)
        let fBound = function(){
            // 这个时候的arguments指的是bind返回的回调函数传入的参数
            let bindArgs = Array.prototype.slice.call(arguments)
            console.log(this instanceof fBound)
            return self.apply(this instanceof fBound? this : context,args.concat(bindArgs))
        }
        fNOP.prototype = this.prototype
        fBound.prototype = new fNOP()
        return fBound 
     }
     var value = 100

     let foo = {
         value:500,
         bar:bar.bind2(null)
     }
     function bar(name,age){
        // this.habit = 'shopping';
        // console.log(name)
        // console.log(age)
        console.log(this.value)
     }
     foo.bar()
    //  bar.prototype.friend = 'kevin';
    //  var bindFoo = bar.bind2(foo,"yukeke")
    //  var obj = new bindFoo(18)
    //  console.log(obj.friend)
    // let result = bar.bind2(foo,"jack");
    // result("26")

























    //  Function.prototype.bind2 = function (context){
    //     let self = this;
    //     let fNOP = function (){}
    //     // 获取bind2函数从第二个参数到最后一个参数
    //     let args = Array.prototype.slice.call(arguments,1)
    //     let fBound = function (){
    //         // 这个时候的arguments 是指bind返回的函数传入的参数
    //         let bindArgs = Array.prototype.slice.call(arguments)
    //         console.log(this instanceof fBound)
    //         return self.apply(this instanceof fBound? this:context, args.concat(bindArgs))
    //     }
    //     fNOP.prototype = this.prototype
    //     fBound.prototype = new fNOP()
    //     return fBound
    //  }
    

    //  Function.prototype.bind2 = function(context){
    //      let self = this;
    //      var sum = Array.prototype.slice.call(arguments,1)
    //      return function (){
    //          var num = Array.prototype.slice.call(arguments)
    //          return self.apply(context,sum.concat(num))
    //      }
    //  }
    //  let result = bar.bind2(foo,"于可可")
    //  result(19)
    //  console.log(result)










    </script>
</body>
</html>